package com.weishu.controller;


import com.alibaba.fastjson.JSON;
import com.gaofei.domain.FileResult;
import com.gaofei.service.FileService;
import com.weishu.domain.TbHouseInfo;
import com.weishu.domain.TbRoomInfo;
import com.weishu.service.ITbHouseInfoService;
import com.weishu.service.ITbRoomInfoService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liyan
 * @since 2021-07-26
 */

@RestController
@RequestMapping("/tbHouseInfo")
public class TbHouseInfoController {

    @Autowired
    private ITbHouseInfoService houseInfoService;
    @Autowired
    private ITbRoomInfoService roomInfoService;
    @Autowired
    private FileService fileService;
    //添加分散式房源
    @RequestMapping("saveHouseRoom")
    public boolean saveHouseRoom(@RequestBody Map<String,Object> map){

        Object house = map.get("house");
        Object room = map.get("room");
        System.err.println("houseinfo====>>"+house);
        System.err.println("roominfo=====>>"+room);

        TbHouseInfo tbHouseInfo = JSON.parseObject(JSON.toJSONString(house), TbHouseInfo.class);
        TbRoomInfo tbRoomInfo = JSON.parseObject(JSON.toJSONString(room), TbRoomInfo.class);
        tbHouseInfo.setId(UUID.randomUUID().toString());
        boolean b=houseInfoService.addhouse(tbHouseInfo);

        if (b){
            tbRoomInfo.setId(UUID.randomUUID().toString());
            b=roomInfoService.addroom(tbRoomInfo);
        }
        return b;
    }
    //查询公共配套
    @RequestMapping("publiclist")
    public List<TbHouseInfo> publiclist(){
        List<TbHouseInfo> list=houseInfoService.publiclist();
        return list;
    }
    //查询房间配套
    @RequestMapping("privatelist")
    public List<TbHouseInfo> privatelist(){
         List<TbHouseInfo> list=houseInfoService.privatelist();
         return list;
    }
    //图片上传
    @RequestMapping(value = "upload",method = RequestMethod.POST)
    @CrossOrigin(methods = {RequestMethod.POST})
    public List<FileResult> upload(MultipartFile file[]){
        List<FileResult> upload = fileService.upload(file);
        System.out.println(upload);
        return upload;
    }
}

